package com.exchange.config;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;

public class KettleEnvironments {

    private KettleDatabaseRepository repository;
    private DatabaseMeta databaseMeta;
    private KettleDatabaseRepositoryMeta kettleDatabaseMeta;
    private RepositoryDirectoryInterface directory;

    // 如果下面属性值不匹配（可添加判断属性, KettleHelper 代码的判断逻辑也要修改），则清除单例对象，重新生成对象返回
    private String db_host;
    private String db_name;
    /*
     * KETTLE初始化*/
    public KettleEnvironments(String db_host,String db_name,String db_port,String db_username,String db_password,
                                              String conn_username,String conn_password) {
        this.db_host = db_host;
        this.db_name = db_name;
        try {
            KettleEnvironment.init();
            repository = new KettleDatabaseRepository();
            //资源库数据库地址，我这里采用mysql数据库
            databaseMeta = new DatabaseMeta("ETL", "MYSQL", "Native",
                    db_host,db_name, db_port, db_username, db_password);
            //资源库元对象,名称参数，id参数，描述等可以随便定义
            kettleDatabaseMeta = new KettleDatabaseRepositoryMeta("ETL", "ERP",
                    "Transformation description", databaseMeta);
            repository.init(kettleDatabaseMeta);
            //连接资源库,资源库用户名和密码
            repository.connect(conn_username, conn_password);
            directory = repository.loadRepositoryDirectoryTree();
        } catch (KettleException e) {
            e.printStackTrace();
        }
    }

    public KettleDatabaseRepository getRepository() {
        return repository;
    }

    public void setRepository(KettleDatabaseRepository repository) {
        this.repository = repository;
    }

    public DatabaseMeta getDatabaseMeta() {
        return databaseMeta;
    }

    public void setDatabaseMeta(DatabaseMeta databaseMeta) {
        this.databaseMeta = databaseMeta;
    }

    public KettleDatabaseRepositoryMeta getKettleDatabaseMeta() {
        return kettleDatabaseMeta;
    }

    public void setKettleDatabaseMeta(KettleDatabaseRepositoryMeta kettleDatabaseMeta) {
        this.kettleDatabaseMeta = kettleDatabaseMeta;
    }

    public RepositoryDirectoryInterface getDirectory() {
        return directory;
    }

    public void setDirectory(RepositoryDirectoryInterface directory) {
        this.directory = directory;
    }

    public String getDb_host() {
        return db_host;
    }

    public void setDb_host(String db_host) {
        this.db_host = db_host;
    }

    public String getDb_name() {
        return db_name;
    }

    public void setDb_name(String db_name) {
        this.db_name = db_name;
    }
}
